local skill = fk.CreateSkill{
  name = "tea__juzheng",
}
---@type TrigSkelSpec<TrigFunc>
local juzheng = {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardUsing then
      return data.card.is_damage_card and player:hasSkill(self) and player ~= target
    else
      return player:hasSkill(self) and player:getMark("@@tea__juzheng_used-turn") > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.CardUsing then
      local room = player.room
      local ids = table.filter(player:getCardIds("he"), function(id)
        return Fk:getCardById(id).type == data.card.type and Fk:getCardById(id).trueName ~= data.card.trueName
      end)
      local c = room:askToCards(player, {
        skill_name = skill.name,
        min_num = 1,
        max_num = 1,
        include_equip = true,
        pattern = tostring(Exppattern{ id = ids }),
        prompt = "#tea__juzheng-choose",
      })
      if #c == 1 then
        event:setCostData(self, c)
        return true
      end
    else
      if #player.room.logic:getActualDamageEvents(1, Util.TrueFunc, Player.HistoryTurn) == 0 then
        event:setCostData(self, player:usedEffectTimes(skill.name, Player.HistoryTurn))
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      room:moveCards{
        ids = event:getCostData(self),
        from = player.id,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        skillName = self.name,
        proposer = player.id,
        moveVisible = true,
      }
      room:setPlayerMark(player, "@@tea__juzheng_used-turn", 1)
      data.tos = {}
    else
      local num = event:getCostData(self)
      local cids = room:askToDiscard(player, {
        skill_name = skill.name,
        min_num = num,
        max_num = num,
        include_equip = true,
        cancelable = false,
        prompt = "#tea__juzheng-discard:::" .. tostring(num),
      })
      if not player.dead and #cids > 0 then
        player:drawCards(#cids, skill.name)
      end
    end
  end,
}
skill:addEffect(fk.CardUsing, juzheng)
skill:addEffect(fk.TurnEnd, juzheng)
return skill